<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Blog\Message;
use Illuminate\Http\Request;

class MessageService
{

    use Service;

    /**
     * 消息首页
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function MessageIndexService( $request )
    {
        $select  = [
            'id', 'content', 'p_user_id', 'user_id', 'static', 'status', 'is_show', 'article_id', 'updated_at',
        ];
        $where   = function ( $query ) use ( $request ) {
            if ( $request->filled('content') ) $query->where('content', 'like', "%" . $request->input('content') . "%");
            if ( $request->filled('status') && $request->input('status') != '-1' ) $query->where('status', $request->input('status'));
            if ( $request->filled('is_show') && $request->input('is_show') != '-1' ) $query->where('is_show', $request->input('is_show'));
        };
        $with    = [
            'user'    => function ( $query ) {
                $query->select('id', 'name');
            },
            'pUser'   => function ( $query ) {
                $query->select('id', 'name');
            },
            'article' => function ( $query ) {
                $query->select('id', 'title');
            },
        ];
        $message = Message::GetModel($where, $with, $select);
        $message->appends([
            'content' => $request->input('content'),
            'status'  => $request->input('status'),
            'is_show' => $request->input('is_show'),
        ])->render();
        return view('blog.message.index', compact('message', 'request'));
    }

    /**
     * 博客消息删除
     *
     * @param         $id
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function MessageDestroyService( $id, $request )
    {
        $ajax = $request->ajax();
        Message::DestroyModel($id);
        if ( $ajax ) return data(NULL, $this->getErrorCode()::message(204));
        else return back()->with('success', $this->getErrorCode()::message(204));
    }

    /**
     * 修改博客消息状态
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function MessageStatusService( $request )
    {
        $select          = [ 'id', 'status', ];
        $where           = function ( $query ) use ( $request ) {
            $query->where('id', $request->input('id'));
        };
        $message         = Message::FirstModel($where, [], $select);
        $message->status = $request->input('status') == 1 ? 2 : 1;
        $message->save();
        return data(NULL, $this->getErrorCode()::message(210));
    }
}
